package com.opal.stockTaking.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.opal.consts.CommonConst;
import com.opal.stockTaking.entity.HSDSummary;
import com.opal.stockTaking.entity.HSDSummaryResultData;
import com.opal.stockTaking.service.HSDSummaryService;
import com.opal.utils.ExcelDownloadUtil;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 收发存汇总表
 */
@Controller
@RequestMapping("/HSDSummary")
public class HSDSummaryController {

    @Autowired
    private HSDSummaryService hsdSummaryService;

    /**
     * 获取easyui的下拉列表:组织
     * @return
     */
    @RequestMapping("getOrg")
    @ResponseBody
    public Object getOrg(){
        return hsdSummaryService.getOrgService();
    }

    /**
     * 获取easyui的combotree(树形下拉列表)组件
     * @return
     */
    @RequestMapping("getStordoc")
    @ResponseBody
    public Object getStordoc(String pkOrg){
        return hsdSummaryService.getStordocService(pkOrg);
    }

    /**
     * 清空'4个表数据'：调用存储过程(内部会插入临时表，往表中cx_ic_sumary生成数据)
     * @return
     */
    @RequestMapping("emptyDate")
    @ResponseBody
    public Object emptyDate(){
        return  hsdSummaryService.emptyDateService();
    }

    /**
     * 按钮'生成数据'；调用存储过程(内部会插入临时表，往表中cx_ic_sumary生成数据)
     * @return
     */
    @RequestMapping("createData")
    @ResponseBody
    public Object createDate(HSDSummary hsdSummary){
      return  hsdSummaryService.createDateService(hsdSummary);
    }

    /**
     * 显示easyui的Datagrid表格数据
     * @param hsdSummary
     * @return
     */
    @RequestMapping("queryData")
    @ResponseBody
    public Object queryData(HSDSummary hsdSummary){
        return hsdSummaryService.queryDataService(hsdSummary);
    }

    @RequestMapping("exportData")
    public void exportData(HttpServletResponse response, HSDSummary hsdSummary, HttpServletRequest request){
        try {
            List<HSDSummaryResultData> list=hsdSummaryService.exportDataService();
            //准备要处理的数据：参数一，设置表头文字等参数，参数二：实体类(带有注解的行头)，参数三：数据库查询出来的数据数据
            Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(),HSDSummaryResultData.class,list);
            //导出excel：设置名字
            ExcelDownloadUtil.ExcelUtil(response, workbook, "收发存汇总表( " +hsdSummary.getStartDate()+"~"+hsdSummary.getEndDate()+ ").xls",request);
        }catch (IOException e){
            e.printStackTrace();
        }
    }


    @RequestMapping("{page}")
    public Object skipPage(String page){
        return "stockTaking/"+page;
    }
}
